import 'package:get/get.dart';
import 'package:xvideos/hj_page/mine_business/ai/gril_friend/ai_h5_page.dart';
import 'package:xvideos/tools_base/debug_log.dart';
import 'package:xvideos/tools_base/loading/loading_alert_widget.dart';
import 'package:xvideos/hj_utils/api_service/ai_service.dart';

import '../../../../hj_utils/global.dart';
import '../mode/ai_gril_resp.dart';
import 'ai_girl_friend_sheet.dart';

class AIGrilFriendLogic extends GetxController {
  AIGirlFriendBalanceModel? balanceModel;
  AIGirlFriendUrlModel? urlModel;
  AIGirlFriendCurrencys? currencys;
  RxBool open = true.obs;
  RxString tips = ''.obs;

  @override
  void onReady() {
    super.onReady();
    loadData();
    onFetchChargeList();
  }

  loadData() async {
    try {
      balanceModel = await AIService.getAiBalance({});
      open.value = true;
    } catch (e) {
      debugLog(e);
      open.value = false;
    }
    update(['balance']);
  }

  //跳转url
  onFetchJumpUrl() async {
    Map<String, dynamic> data = {};
    try {
      LoadingAlertWidget.show(Get.context!);
      urlModel = await AIService.getAiFriendUrl(data);
      LoadingAlertWidget.cancel(Get.context!);
    } catch (e) {
      LoadingAlertWidget.cancel(Get.context!);
      debugLog(e);
    }
    if (urlModel?.url != null) {
      await Get.to(AiH5Page(webUrl: urlModel?.url));
      loadData();
    } else {
      showToast('未知链接');
    }
  }

  //充值操作
  onRechargeBottomSheet() async {
    if (currencys != null && currencys?.list?.isNotEmpty == true) {
      Get.bottomSheet(AIGrilfriendSheet(list: currencys?.list), isScrollControlled: true);
    } else {
      await onFetchChargeList();
      if (currencys?.list?.isNotEmpty != true) {
        showToast("暂未配置充值数据");
      }
    }
  }

  //获取充值列表
  Future onFetchChargeList({bool showLoading = false}) async {
    if (showLoading) LoadingAlertWidget.show(Get.context!);
    try {
      currencys = await AIService.getAiFriendCurrencys();
    } catch (e) {
      debugLog(e);
    }
    if (showLoading) LoadingAlertWidget.cancel(Get.context!);
  }
}
